<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
    	$res =DB::table('users')->orderBy('id','desc')->paginate(5);
      	return view('admin.user_index',['res'=>$res]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.user_add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
    	//验证
       	$this->validate($request,[
       		'name'         =>  'required',		
       		'email'         =>  'required|unique:users',
       		'mobile'       =>  'required',
       		'password'   =>  'required',
       	],[
       		'unique'	=>  ':attribute 只能存在一个'	
       	]);
       	//构造数据
       	$user['name']          = $request->name;
       	$user['password']    = bcrypt($request->password);
       	$user['mobile']        = $request->mobile;
       	$user['email']          = $request->email;
       	
       	//插入数据库
       	DB::table('users')->insert($user);
       	return redirect('admin/user');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
    	$res = DB::table('users')->where('id',$id)->first();
        return view('admin.user_show',['res'=>$res]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
    	$res = DB::table('users')->where('id',$id)->first();
        return view('admin.user_edit',['res'=>$res]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
    	//验证
       	$this->validate($request,[
       		'name'         =>  'required',                        
       		'email'         =>  'required',
       		'mobile'       =>  'required',
       		'password'   =>  'required',
       	]);
       	//构造数据
       	$user['name']          = $request->name;
       	$user['password']    = bcrypt($request->password);
       	$user['mobile']        = $request->mobile;
       	$user['email']          = $request->email;
        
        //更新
        DB::table('users')->where('id',$id)->update($user);
        return redirect('admin/user');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
    
    }
    
    /**
     * 单个删除
     */
    public function  delete(Request $request)
    {
    	$id = $request->id;
    	$res = DB::table('users')->where('id',$id)->delete();
     	return response()->json($res);
    }
    /*
     * 批量删除
     */
    public function groupdel()
    {
    	return view('admin.user_groupdel');
    }
}
